/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolóxico de Galicia
 * Copyright (C) 2010-2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.orders;

import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.web.common.Util;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Datebox;

import java.util.Date;

import static org.libreplan.web.I18nHelper._;

/**
 * Controller for {@link OrderElement} details.
 *
 * @author Diego Pino García <dpino@igalia.com>
 * @author Vova Perebykivskyi <vova@libreplan-enterprise.com>
 */
public class DetailsOrderElementController extends GenericForwardComposer {

    private IOrderElementModel orderElementModel;

    private Datebox initDate;

    private Datebox deadline;

    public DetailsOrderElementController() {
        if ( orderElementModel == null ) {
            orderElementModel = (IOrderElementModel) SpringUtil.getBean("orderElementModel");
        }
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        comp.setAttribute("detailsController", this, true);
    }

    public void setOrderElementModel(IOrderElementModel orderElementModel) {
        this.orderElementModel = orderElementModel;
    }

    public OrderElement getOrderElement() {
        return orderElementModel.getOrderElement();
    }

    public void openWindow(IOrderElementModel model) {
        setOrderElementModel(model);
        deadline.setDisabled(isSubcontracted());
        Util.reloadBindings(self);
    }

    public boolean isCodeAutogenerated() {
        return orderElementModel.isCodeAutogenerated();
    }

    public boolean isContainer() {
        return orderElementModel.getOrderElement() != null && !orderElementModel.getOrderElement().isLeaf();
    }

    private boolean isSubcontracted() {
        return !(orderElementModel.getOrderElement() == null ||
                orderElementModel.getOrderElement().getTaskSource() == null) &&
                orderElementModel.getOrderElement().getTaskSource().getTask().isSubcontracted();
    }

    public String getMoneyFormat() {
        return Util.getMoneyFormat();
    }

    public boolean isCodeDisabled() {
        if ( isCodeAutogenerated() ) {
            return true;
        }
        OrderElement orderElement = orderElementModel.getOrderElement();

        return orderElement != null && orderElement.isJiraIssue();
    }

    /**
     * Should be public!
     * Used in _orderElementDetails.zul
     */
    public Constraint checkConstraintStartDate() {
        return new Constraint() {
            @Override
            public void validate(Component comp, Object value) throws WrongValueException {
                Date startDate = (Date) value;
                Date year2010 = new Date(1262296800000L);

                boolean startBefore2010 = (startDate != null) && startDate.before(year2010);

                if ( startBefore2010 ) {
                    initDate.setValue(null);
                    getOrderElement().setInitDate(null);
                    throw new WrongValueException(comp, _("Must be after 2010!"));
                }
            }
        };
    }

    /**
     * Should be public!
     * Used in _orderElementDetails.zul
     */
    public Constraint checkConstraintFinishDate() {
        return new Constraint() {
            @Override
            public void validate(Component comp, Object value) throws WrongValueException {
                Date finishDate = (Date) value;

                boolean deadlineBeforeStart = (finishDate != null) &&
                        (initDate.getValue() != null) &&
                        (finishDate.compareTo(initDate.getValue()) < 0);

                if ( deadlineBeforeStart ) {
                    deadline.setValue(null);
                    getOrderElement().setDeadline(null);
                    throw new WrongValueException(comp, _("must be after starting date"));
                }
            }
        };
    }

}